// UVa167 The Sultan's Successors
// 刘汝佳
// 算法：枚举全排列，然后判断是否有两个皇后在同一条对角线上
#include <algorithm>
#include <cstdio>
using namespace std;
const int SZ = 8;
int v[SZ][SZ], P[SZ];

bool judge() {
  bool ok = true;
  for (int i = 0; i < SZ; i++)
    for (int j = 0; j < i; j++)
      if (P[i] - i == P[j] - j || P[i] + i == P[j] + j) return false;
  return true;
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    for (int i = 0; i < SZ; i++) {
      P[i] = i;
      for (int j = 0; j < SZ; j++) scanf("%d", &v[i][j]);
    }
    int ans = 0;
    do {
      if (!judge()) continue;
      int sum = 0;
      for (int i = 0; i < SZ; i++) sum += v[i][P[i]];
      ans = max(ans, sum);
    } while (next_permutation(P, P + SZ));
    printf("%5d\n", ans);
  }
  return 0;
}
/*
算法分析请参考: 《入门经典-第2版》7.4.1 八皇后问题
注意本题中通过调用next_permutation避免了手写递归过程
*/
// 25889710  167  The Sultan's Successors  Accepted  C++  0.000  2020-12-26 13:04:38